package com.sise.thread.lab01.lab02;

class lock01 implements Runnable{

    public static int i=0;
    public static boolean flag=true;
    public Object obj=new Object();
    @Override
    public void run() {
        if(flag==true){
            while(true){
                synchronized (obj) {
                    try {
                        icount();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }else{
            while(true){
                try {
                    icount();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public synchronized void icount() throws InterruptedException {
        synchronized (obj){
            Thread.sleep(40);
            i++;
            System.out.println("这是"+Thread.currentThread().getName()+i);
        }
    }
}

//模拟死锁状态
public class DeadLock {
    public static void main(String[] args) throws InterruptedException {
        lock01 k1=new lock01();
        Thread thread=new Thread(k1,"locak1");
        Thread thread2=new Thread(k1,"locak2");
        thread.start();
        Thread.sleep(50);
        lock01.flag=false;
        thread2.start();
    }
}
